/*
 * ----------------------
 * Includes
 * ----------------------
 */

 // Standard
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// SDL
#include <SDL.h>
#include <SDL/SDL_ttf.h>

// GUI
#include "elder_gui.h"

// Context
#include "context.h"

/*
 * ----------------------
 * Global variables
 * ----------------------
 */

// Screen surface
SDL_Surface *screen = NULL;

// Images
SDL_Surface *imgBackground = NULL;

// Current context
extern context currentContext;

/*
 * ----------------------
 * Function definitions
 * ----------------------
 */

// Draw background
void drawBackground(void)
{
	// Background main picture
	SDL_BlitSurface(imgBackground, NULL, screen, 0);


    // Just for tests
    if (currentContext.batteries.el1Voltage <=1000)
        currentContext.batteries.el1Voltage=1400;
    else
        currentContext.batteries.el1Voltage = currentContext.batteries.el1Voltage-10;
	updateBatteries();

	// Draw textual elements
	drawTextElements();



}

// Load all images needed by the GUI
static int loadImages(void)
{
    int result = 0;

    imgBackground  = IMG_Load("./data/images/gui.png");

    // Check if we got all images
    if( imgBackground == NULL )
    {
        fprintf(stderr, "[SDL] Unable to load image files !\n");
        result = -1;
    }


    return(result);
}

// Init system modules
static int initSystem(void)
{
	int result = 0;
    #ifdef X86
	// Initialize Backlight control
    if( initBacklightControl() )
    {
        fprintf(stderr, "[SDL] Unable to find Backlight control on your system -> won't be activated\n");
        return -1;
    }
    #endif
}

// Free ressources
static void freeRessources(void)
{
    SDL_FreeSurface(screen);
    SDL_FreeSurface(imgBackground);
    closeFonts();
}

// Cleanup
void cleanup(void)
{
    // Cleanup what we used
    //releaseBacklightControl();
    freeRessources();
    SDL_Quit();
    TTF_Quit();
}

// Main
int main(int argc, char *argv[])
{
    // Initialize SDL
    // SDL_INIT_EVENTTHREAD, SDL_INIT_NOPARACHUTE
    if(SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER)<0)
    {
        fprintf(stderr, "[SDL] Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
    }

    // Ask SDL to cleanup when exiting
    atexit(cleanup);

    // Get a screen to display GUI
    // SDL_HWPALETTE, SDL_HWSURFACE, SDL_DOUBLEBUF
    if((screen=SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_ANYFORMAT))==NULL)
    {
        fprintf(stderr, "[SDL] Unable to set %dx%d video mode: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError());
        exit(1);
    }

    // Initialize SDL_TTF
    if (TTF_Init() == -1)
	{
	  fprintf(stderr, "[SDL] Unable to initialize SDL_ttf: %s \n", TTF_GetError());
	  exit(1);
	}

    // Load our graphics and fonts
    if(loadImages() || loadFonts())
    {
        return 1;
    }

    // Initialize system modules
    if(initSystem())
    {
        #ifndef X86
    	return 1;
    	#endif
    }

    // Init context
	initContext();

    // Show mouse
    SDL_ShowCursor(1);

    // Draw background
    drawBackground();



    // Main loop, wait for event
    // Return when programm exited
    eventManager();


    return 0;
}


